/*!
 * Copyright (c) 2023-present StarDreamNet
 */
(document.loader || (document.loader = [])).push(["index", class {
    constructor() { };
    dateSpanString(date1, date2) {
        var date1 = new Date(date1).getTime();
        var date2 = new Date(date2).getTime();
        var span = Math.abs(date2 - date1) / 1000;
        if (span < 60) {
            return "刚刚";
        } else if (span < 3600) {
            return Math.floor(span / 60) + "分钟前";
        } else if (span < 86400) {
            return Math.floor(span / 3600) + "小时前";
        } else if (span < 2592000) {
            return Math.floor(span / 86400) + "天前";
        } else if (span < 31104000) {
            return Math.floor(span / 2592000) + "个月前";
        } else {
            return Math.floor(span / 31104000) + "年前";
        }
    };
    resize() {
        $(".desktop").removeClass("fixed")
        if ($(document.documentElement).prop("scrollHeight") - $(window).height() <= 0) {
            $(".desktop").addClass("fixed")
        } else {
            $(".desktop").removeClass("fixed")
        }
    };
    initMobile() {
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
            $(".app").addClass("mobile")
        }
    };
    initSW() {
        if (!'serviceWorker' in navigator) return console.log("sw: 浏览器不支持 ServiceWorker");
        // navigator.serviceWorker.register("/sw.js");
        // 删除以前的sw
        navigator.serviceWorker.getRegistrations().then(function (registrations) {
            for (let registration of registrations) {
                if (registration.active.scriptURL == "https://ducksite.cloudroo.top/sw.js")
                    registration.unregister()

            }
        })
    };
    initSwalSimple() {
        if (typeof swal == 'undefined') return;

        Object.assign(swal, {
            skip(config) {
                config = config || {};

                var num = (config.num || 3);

                function upd() {
                    $(".swal-text").text(`将在 ${num || 0}${config.unit || "s"} 后跳转到 ${config.to || "首页"}`)
                }
                function to() {
                    location.href = config.src || "/";
                }

                swal({
                    icon: config.icon || "success",
                    title: config.title || "成功",
                    text: " ",
                    buttons: {
                        confirm: {
                            text: "立即跳转",
                            value: true,
                            visible: true,
                            className: "",
                            closeModal: true
                        }
                    },
                    closeOnClickOutside: false,
                }).then(v => {
                    if (v) {
                        to();
                    }
                });

                setInterval(() => {
                    num--;
                    upd();
                    if (num == 0) {
                        to();
                    }
                }, (config.mul || 1000));

                upd();
            },
        });
    }
    init_(require) {
        this.initSW();
        this.resize();
        this.initMobile();
        this.initSwalSimple();
        $(window).on("resize", this.resize);
    };
}]);

// Modules Loader
$(document).ready(function () {
    const modules = new Map();
    const require = name => {
        var module = modules.get(name);
        return module || null;
    };
    const addModule = (name, content) => {
        if (modules.has(name)) {
            throw Error("The Module name" + String(name) + " has been named.");
        } else {
            try {
                modules.set(name, content);
            } catch (e) {
                throw Error("Module " + String(name) + " failed to load.\n"+e);
            }
        }
    };
    (document.loader || (document.loader = [])).forEach(module => {
        try {
            var name = module[0];
            var content = module[1];
            if (typeof content === 'function') {
                if (content.prototype && content.prototype.constructor && content.prototype.constructor.toString().startsWith('class')) {
                    var x = new content();
                    addModule(name, x);
                    x.init_(require);
                } else {
                    var exp = null;
                    var mod = { exports: exp }
                    content(require, exp, mod);
                    addModule(name, exp || mod.exports);
                }
            } else if (typeof content === 'object') {
                addModule(name, content);
                content.init_(require);
            } else {
                addModule(name, content);
            }
        } catch (e) {
            throw Error("Module " + String(name) + " failed to load.\n" + e);
        }
    });
});